package com.pickgliss.ui
{
	import com.pickgliss.events.ComponentEvent;
	import com.pickgliss.toplevel.StageReferance;
	import com.pickgliss.ui.controls.alert.BaseAlerFrame;
	import com.pickgliss.ui.vo.AlertInfo;
	import com.pickgliss.utils.ObjectUtils;
	import com.pickgliss.utils.StringUtils;
	
	import flash.events.Event;
	
	import mx.utils.ObjectUtil;

	public final class AlertManager
	{
		private static var _instance:AlertManager;
		public static var DEFAULT_CONFIRM_LABEL:String="确定";
		public static function get Instance():AlertManager
		{
			if(_instance == null)
			{
				_instance = new AlertManager();
			}
			return _instance;
		}
		
		public function AlertManager()
		{
		}
		private var _layerType:int;
		public function set layerType(value : int) : void
		{
			_layerType = value;//掉线时不可清，强弹到最上层
		}
		private var _simpleAlertInfo:AlertInfo;
		/**
		 * 
		 * @param frameStyle 弹窗的样式
		 * @param info 弹出的数据
		 * @param blockBackgound 默认值为LayerManager.ALPHA_BLOCKGOUND
		 * @return 弹窗
		 * 
		 */		
		public function alert(frameStyle:String,info:AlertInfo,blockBackgound:int = 2):BaseAlerFrame
		{
			var alert:BaseAlerFrame = ComponentFactory.Instance.creat(frameStyle);
			alert.addEventListener(ComponentEvent.PROPERTIES_CHANGED,__onAlertSizeChanged);
			alert.addEventListener(Event.REMOVED_FROM_STAGE,__onAlertRemoved);
			alert.info = info;
			LayerManager.Instance.addToLayer(alert,_layerType,alert.info.frameCenter,true,blockBackgound);
			StageReferance.stage.focus = alert;
			return alert;
		}

		private function __onAlertRemoved(event:Event):void
		{
			var alert:BaseAlerFrame = event.currentTarget as BaseAlerFrame;
			alert.removeEventListener(ComponentEvent.PROPERTIES_CHANGED,__onAlertSizeChanged);
			alert.removeEventListener(Event.REMOVED_FROM_STAGE,__onAlertRemoved);
		}

		private function __onAlertSizeChanged(event:ComponentEvent):void
		{
			var alert:BaseAlerFrame = event.currentTarget as BaseAlerFrame;
			if(alert.info.frameCenter)
			{
				alert.x = (StageReferance.stageWidth - alert.width)/2;
				alert.y = (StageReferance.stageHeight - alert.height)/2;
			}
		}
		/**
		 * 
		 * @param container 弹出窗层的容器
		 * @param simpleAlertInfo 简单弹出窗的info
		 * 
		 */		
		public function setup(layerType:int,simpleAlertInfo:AlertInfo):void
		{
			_simpleAlertInfo = simpleAlertInfo;
			_layerType = layerType;
		}
		/**
		 * 
		 * @param msg 弹出窗信息
		 * @param title 弹出窗标题
		 * @param submitLabel 确定文本
		 * @param cancelLabel 取消文本
		 * @return 弹出窗
		 * 
		 */		
		public function simpleAlert(title:String,msg:String,submitLabel:String="", cancelLabel:String="",autoDispose:Boolean=false,enableHtml:Boolean=false,multiLine:Boolean=false,blockBackgound:int = 2,smallTip:String = "",smallTipCall:Function = null):BaseAlerFrame
		{
			if(StringUtils.isEmpty(submitLabel)) submitLabel = DEFAULT_CONFIRM_LABEL;
			var alerInfo:AlertInfo = new AlertInfo();
			ObjectUtils.copyProperties(alerInfo,_simpleAlertInfo);
			alerInfo.closeSoundFunc = _simpleAlertInfo.closeSoundFunc;
			alerInfo.submitSoundFunc = _simpleAlertInfo.submitSoundFunc;
			alerInfo.data = msg;
			alerInfo.autoDispose = autoDispose;
			alerInfo.title = title;
			alerInfo.submitLabel = submitLabel;
			alerInfo.cancelLabel = cancelLabel;
			alerInfo.enableHtml = enableHtml;
			alerInfo.mutiline = multiLine;
			alerInfo.smallTip = smallTip;
			alerInfo.smallTipCall = smallTipCall;
			if(StringUtils.isEmpty(cancelLabel))alerInfo.showCancel = false;
			return alert(ComponentSetting.SIMPLE_ALERT_STYLE,alerInfo,blockBackgound);
		}
		/**
		 * 功能提示框 
		 * @param title
		 * @param msg
		 * @param submitLabel
		 * @param cancelLabel
		 * @param autoDispose
		 * @param enableHtml
		 * @param multiLine
		 * @param blockBackgound
		 * 
		 */		
		public function functionAlert(title:String,msg:String,submitLabel:String="", cancelLabel:String="",autoDispose:Boolean=false,enableHtml:Boolean=false,multiLine:Boolean=false,blockBackgound:int = 2,smallTip:String = "",smallTipCall:Function = null):BaseAlerFrame
		{
			if(StringUtils.isEmpty(submitLabel)) submitLabel = DEFAULT_CONFIRM_LABEL;
			var alerInfo:AlertInfo = new AlertInfo();
			ObjectUtils.copyProperties(alerInfo,_simpleAlertInfo);
			alerInfo.closeSoundFunc = _simpleAlertInfo.closeSoundFunc;
			alerInfo.submitSoundFunc = _simpleAlertInfo.submitSoundFunc;
			alerInfo.textShowWidth = 350;
			alerInfo.data = msg;
			alerInfo.autoDispose = autoDispose;
			alerInfo.title = title;
			alerInfo.submitLabel = submitLabel;
			alerInfo.cancelLabel = cancelLabel;
			alerInfo.enableHtml = enableHtml;
			alerInfo.mutiline = multiLine;
			alerInfo.smallTip = smallTip;
			alerInfo.smallTipCall = smallTipCall;
			if(StringUtils.isEmpty(cancelLabel))alerInfo.showCancel = false;
			return alert(ComponentSetting.FUNCTION_ALERT_STLE,alerInfo,blockBackgound);
		}
	}
}